package com.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.blog.filter.SensitiveFilter;
import com.blog.pojo.Comment;
import com.blog.pojo.User;
import com.blog.service.CommentService;
import com.blog.utils.QueryPageBean;
import com.blog.utils.UUIDUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.util.List;

/**
 * Copyright (C), 2022
 * Author: 苏大大
 * Date: 2022-05-23 14:58
 * FileName: BlogTagController
 * Description: 博客评论控制层
 */
@RequestMapping("/comment")
@RestController
public class CommentController {
    @Resource
    private CommentService commentService;

    /***
     * 根据博客id查询博客评论
     * @param blogId
     * @return
     */
    @GetMapping("/commentList/{blogId}")
    public String getCommentList(@PathVariable("blogId") Long blogId) {
        JSONObject object=new JSONObject();
        boolean flag=commentService.getCommentList(blogId)!=null;
        object.put("flag",flag);
        object.put("commentList",commentService.getCommentList(blogId));
        return object.toJSONString();
    }

    /***
     * 回复评论
     * @param comment
     * @param request
     * @return
     */
    @PostMapping("/admin/replyComment/{uid}")
    public String replyComment(@RequestBody Comment comment, HttpServletRequest request,@PathVariable("uid") Long uid) {
        JSONObject object=new JSONObject();
        comment.setCommentId(UUIDUtils.getNumber());
        //敏感词过滤
        String content=SensitiveFilter.filter(comment.getContent().trim());
        //判断是否包含敏感词
        if(content.equals(comment.getContent())){
            object.put("flag2",false);
        }else{
            object.put("flag2",true);
        }
         object.put("flag",commentService.replyComment(comment,uid));
         object.put("message","评论内容包含敏感词");
        return object.toJSONString();
    }

    /***
     * 用户删除评论
     * @param blogId
     * @param commentId
     * @param request
     * @return
     */
    @DeleteMapping("/admin/del/{blogId}/{commentId}/{uid}")
    public String delComment(@PathVariable Long blogId, @PathVariable Long commentId,@PathVariable("uid") Long uid, HttpServletRequest request) {
        JSONObject object=new JSONObject();
        if (commentService.delComment(blogId, commentId, uid)){
            object.put("flag",true);
            object.put("message","删除评论成功!");
            return object.toJSONString();
        }
        object.put("message","您删除的评论不是你发布的，你无权删除！");
        return object.toJSONString();
    }

    /***
     * "删除评论
     * @param commentIdList
     * @return
     */
    @DeleteMapping("/admin/delete")
    public String adminDelComment(@RequestBody List<Long> commentIdList) {
        JSONObject object=new JSONObject();
        commentService.removeByIds(commentIdList);
        object.put("message","删除评论成功");
        return object.toJSONString();
    }

}
